package com.cnfangmao.masterdata.controller;

import javax.validation.constraints.NotNull;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cnfangmao.masterdata.common.response.ResBody;
import com.cnfangmao.masterdata.common.response.ResCodeEnum;
import com.cnfangmao.masterdata.common.util.ModelUtils;
import com.cnfangmao.masterdata.common.util.SnowFlakeUtils;
import com.cnfangmao.masterdata.dto.BasicDataFieldMetadataDto;
import com.cnfangmao.masterdata.dto.BasicDataFieldMetadataSearchDto;
import com.cnfangmao.masterdata.dto.GeneralSearchDto;
import com.cnfangmao.masterdata.model.BasicDataFieldMetadata;
import com.cnfangmao.masterdata.service.BasicDataFieldMetadataService;
import com.cnfangmao.masterdata.vo.GeneralPageVo;

/**
 * <p>
 * 字段元数据表 前端控制器
 * </p>
 *
 * @author chenxd
 * @since 2020-04-14
 */
@RequestMapping("/basicDataFieldMetadata")
@Api(tags = "1-1、配置-字段元数据管理")
@RestController
public class BasicDataFieldMetadataController {

	private static final Logger logger = LoggerFactory.getLogger(BasicDataFieldMetadataController.class);

	@Autowired
	private BasicDataFieldMetadataService basicDataFieldMetadataService;

	/**
	 * 字段元数据列表
	 */
	@RequestMapping(value = "/list.do", method = RequestMethod.POST)
	@ApiOperation(value = "字段元数据列表", notes = "")
	@ApiImplicitParam(name = "dto", value = "字段元数据列表查询条件", required = true, paramType = "body", dataType = "GeneralSearchDto«BasicDataFieldMetadataSearchDto»")
	public ResBody<GeneralPageVo<BasicDataFieldMetadata>> listBasicDataFieldMetadata(@RequestBody GeneralSearchDto<BasicDataFieldMetadataSearchDto> dto) {
		ResBody<GeneralPageVo<BasicDataFieldMetadata>> resBody = new ResBody<GeneralPageVo<BasicDataFieldMetadata>>();
		try {
			IPage<BasicDataFieldMetadata> data = basicDataFieldMetadataService.queryListBasicDataFieldMetadata(dto);
			GeneralPageVo<BasicDataFieldMetadata> result= new GeneralPageVo<BasicDataFieldMetadata>();
			result.setPageNo(data.getCurrent());
			result.setPageSize(data.getPages());
			result.setRecords(data.getRecords());
			result.setTotalCount(data.getTotal());
			resBody.code(ResCodeEnum.SUCCESS.getCode());
			resBody.data(result);
			resBody.message("查询成功");
		} catch (Exception e) {
			logger.error(String.format("run list error in BasicDataFieldMetadataController: %s", e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}

		return resBody;
	}

	/**
	 * 创建字段元数据
	 */
	@RequestMapping(value = "/create.do", method = RequestMethod.POST)
	@ApiOperation(value = "创建字段元数据", notes = "")
	@ApiImplicitParam(name = "input", value = "创建主字段元数据信息", required = true, paramType = "body", dataType = "BasicDataFieldMetadataDto")
	public ResBody<Boolean> createBasicDataFieldMetadata(@Validated @RequestBody @NotNull BasicDataFieldMetadataDto input) {
		ResBody<Boolean> resBody = new ResBody<Boolean>();
		try {
			if (input.getFieldCode().isEmpty() || input.getFieldName().isEmpty() ) {
				resBody.code(ResCodeEnum.PARAM_NULL.getCode());
				resBody.message("字段英文名称或者字段中文名称为空");
				return resBody;
			}
			if (basicDataFieldMetadataService.findByFieldMetadataName(input.getFieldName()) != null) {
				resBody.code(ResCodeEnum.PARAM_NULL.getCode());
				resBody.message("字段中文名称已经存在");
				return resBody;
			}
			if (basicDataFieldMetadataService.findByFieldMetadataCode(input.getFieldName()) != null) {
				resBody.code(ResCodeEnum.PARAM_NULL.getCode());
				resBody.message("字段英文名称已经存在");
				return resBody;
			}
			input.setFieldId(SnowFlakeUtils.generator());
			BasicDataFieldMetadata entity= ModelUtils.parseObject(input, BasicDataFieldMetadata.class, "create");
			basicDataFieldMetadataService.save(entity);
			resBody.code(ResCodeEnum.SUCCESS.getCode());
			resBody.data(true);
			resBody.message("创建成功");
		} catch (Exception e) {
			logger.error(String.format("run list create in BasicDataFieldMetadataController: %s", e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
		
		return resBody;
	}

	/**
	 * 更新字段元数据信息
	 */
	@RequestMapping(value = "/update.do", method = RequestMethod.POST)
	@ApiOperation(value = "更新字段元数据", notes = "")
	@ApiImplicitParam(name = "input", value = "更新字段元数据信息", required = true, paramType = "body", dataType = "BasicDataFieldMetadataDto")
	public ResBody<Boolean> updateBasicDataFieldMetadata(@Validated @RequestBody @NotNull BasicDataFieldMetadataDto input) {
		ResBody<Boolean> resBody = new ResBody<Boolean>();
		try {
			if (input.getFieldCode().isEmpty() || input.getFieldName().isEmpty() ) {
				resBody.code(ResCodeEnum.PARAM_NULL.getCode());
				resBody.message("字段英文名称或者字段中文名称为空");
				return resBody;
			}
			BasicDataFieldMetadata fieldName= basicDataFieldMetadataService.findByFieldMetadataName(input.getFieldName());
			if (fieldName != null && !fieldName.getFieldId().equals(input.getFieldId())) {
				resBody.code(ResCodeEnum.PARAM_NULL.getCode());
				resBody.message("字段中文名称已经存在");
				return resBody;
			}
			BasicDataFieldMetadata fieldCode= basicDataFieldMetadataService.findByFieldMetadataCode(input.getFieldCode());
			if (fieldCode != null && !fieldCode.getFieldId().equals(input.getFieldId())) {
				resBody.code(ResCodeEnum.PARAM_NULL.getCode());
				resBody.message("字段英文名称已经存在");
				return resBody;
			}
			BasicDataFieldMetadata entity = ModelUtils.parseObject(input, BasicDataFieldMetadata.class, "update");
			basicDataFieldMetadataService.updateById(entity);
			resBody.code(ResCodeEnum.SUCCESS.getCode());
			resBody.data(true);
			resBody.message("修改成功");
		} catch (Exception e) {
			logger.error(String.format("run list update in BasicDataFieldMetadataController: %s", e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
		
		return resBody;
	}

	/**
	 * 删除字段元数据
	 * @param userId
	 * @return
	 */
	@RequestMapping(value = "/delete.do", method = RequestMethod.POST)
	@ApiOperation(value = "删除字段元数据", notes = "")
	@ApiImplicitParam(name = "fieldId", value = "字段元数据fieldId",paramType = "body", required = true, dataType = "string")
	public ResBody<Boolean> deleteBasicDataFieldMetadata(@Validated @RequestBody @NotNull String fieldId) {
		ResBody<Boolean> resBody = new ResBody<Boolean>();
		try {
			basicDataFieldMetadataService.removeById(fieldId);
			resBody.code(ResCodeEnum.SUCCESS.getCode());
			resBody.data(true);
			resBody.message("删除成功");
		} catch (Exception e) {
			logger.error(String.format("run list delete in BasicDataFieldMetadataController: %s", e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
		return resBody;
	}
	
	/**
     * 获取字段元数据详情
     */
    @RequestMapping(value="/getBasicDataFieldMetadata.do", method = RequestMethod.POST)
    @ApiOperation(value="获取字段元数据详情", notes = "")
    @ApiImplicitParam(name = "fieldId", value = "字段元数据fieldId", required = true, paramType = "body",dataType = "string")
    public ResBody<BasicDataFieldMetadata> getBasicDataFieldMetadata(@Validated @RequestBody @NotNull String fieldId){
    	ResBody<BasicDataFieldMetadata> resBody =new ResBody<BasicDataFieldMetadata>();
    	try {
        	BasicDataFieldMetadata basicDataFieldMetadata=basicDataFieldMetadataService.getById(fieldId);
        	resBody.code(ResCodeEnum.SUCCESS.getCode());
			resBody.data(basicDataFieldMetadata);
		} catch (Exception e) {
			logger.error(String.format("run list getBasicDataFieldMetadata in BasicDataFieldMetadataController: %s", e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
    	return resBody;
    }

}

